import { response } from "express";
import { request } from "express";
import CODE from "../../config/data.config.js";
import * as service from '../../service/service.js'

class UserController {
    /**
     * @param {request} req
     * @param {response} res
     */
    static async GetAllUser(req, res, next) {
        try {
            const { condition, onlyIsFreeze } = req.query;
            const result = await service.UserService.GetAll(condition, onlyIsFreeze)
            res.send({
                msg: '',
                code: CODE.CODE_OK,
                data: result
            }).end()
        } catch (error) {
            next(error)
        }
    }

    /**
     * @param {request} req
     * @param {response} res
     */
    static async InsertUser(req, res, next) {
        try {
            const body = req.body;
            const result = await service.UserService.InsertUser(body);
            res.send(result).end()
        } catch (error) {
            next(error)
        }
    }

    /**
     * @param {request} req
     * @param {response} res
     */
    static async DeleteUser(req, res) {
        try {
            const body = req.body;
            const result = await service.UserService.DeleteUser(body);
            res.send(result).end()
        } catch (error) {
            res.write(error)
            res.status(403)
            res.end()
        }
    }

    /**
     * @param {request} req
     * @param {response} res
     */
    static async GetPortrait(req, res, next) {
        try {
            const { userID } = req.query
            const portraitSrc = await service.UserService.GetPortrait(userID);
            if (portraitSrc) {
                res.send({
                    code: 1,
                    meg: '获取头像成功',
                    data: {
                        portraitSrc
                    }
                })
            }
        } catch (error) {
            next({ error })
        }
    }
    /**
     * @param {request} req
     * @param {response} res
     */
    static async GetStar(req, res, next) {
        try {
            const { userID } = req.query
            const result = await service.UserService.GetStar(userID);
            if (result) {
                res.send({
                    code: 1,
                    meg: '获取数据成功',
                    data: result
                })
            }
        } catch (error) {
            next({ error })
        }
    }

    /**
     * @param {request} req
     * @param {response} res
     */
    static async EditUser(req, res, next) {
        try {
            let { model, columns } = req.body
            let state = await service.UserService.EditUser(model, columns)
            res.send({
                msg: '',
                code: CODE.CODE_OK,
                data: state
            })
        } catch (error) {
            next(error)
        }
    }

    /**
     * 获取个人用户信息
     * @param {request} req
     * @param {response} res
     */
    static async GetUserInfo(req, res, next) {
        try {
            let { phone } = req.query
            let result = await service.UserService.GetUserInfo(phone)
            res.send({
                msg: '',
                code: CODE.CODE_OK,
                data: result
            })
        } catch (error) {
            next(error)
        }
    }
}


export default UserController;